package com.fcscs.cservice.client.main.configuration.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.TransactionType;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.TransactionTypeModelData;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class TransactionTypeGrid extends SimpleGridPanel<TransactionTypeModelData> implements
		FormSubmitListener<TransactionType>, ModelSelectListener<TransactionTypeModelData> {
	private static TransactionTypeGrid INSTANCE;
	private TransactionTypeDialog transactionTypeDialog;

	public TransactionTypeGrid(String heading, ModelSelectListener<TransactionTypeModelData> l) {
		super(heading, l, (Object) null);
		setCollapsible(false);
		registerListeners(this);
		setSize(650, 520);
	}

	public static TransactionTypeGrid getInstance(String title,
			ModelSelectListener<TransactionTypeModelData> modelSelectListener) {
		if (null == INSTANCE) {
			INSTANCE = new TransactionTypeGrid(title, modelSelectListener);
		}
		return INSTANCE;
	}

	@Override
	protected ColumnModel createColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		ColumnConfig cc = CoreUtil.createNonSortableColumnConf(CoreConstants.FULLTNAME,
				CServiceConstants.CSERVICE_CONSTANTS.transactionType(), 150);
		columns.add(cc);

		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.CODE, CoreConstants.CORE_CONSTANTS.code(),
				150);
		columns.add(cc);

		cc = CoreUtil.createNonSortableColumnConf(CServiceConstants.C_CONF,
				CServiceConstants.CSERVICE_CONSTANTS.revenue(), 150);
		columns.add(cc);
		
		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
				CoreConstants.CORE_CONSTANTS.action(), 150);
		cc.setRenderer(new GridCellRenderer<TransactionTypeModelData>() {
			public Object render(final TransactionTypeModelData model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<TransactionTypeModelData> store,
					final Grid<TransactionTypeModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);
				HighLightIconButton ib = createEditBtn(model);
				ib.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
				hr.add(ib);
				return hr;
			}
		});
		columns.add(cc);
		ColumnModel columnModel = new ColumnModel(columns);
		return columnModel;
	}
	
	public HighLightIconButton createEditBtn(TransactionTypeModelData model) {
		HighLightIconButton editBtn = new HighLightIconButton(ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {

					@Override
					public void componentSelected(IconButtonEvent ce) {
						transactionTypeDialog = TransactionTypeDialog.getInstance(false);
						transactionTypeDialog.show();
					}
				});
		editBtn.setTitle(CoreConstants.CORE_CONSTANTS.delete());
		return editBtn;
	}

	@Override
	public void onModelClick(TransactionTypeModelData model) {

	}

	@Override
	public void onModelDoubleClick(TransactionTypeModelData model) {

	}

	@Override
	public void onModelEnterPress(TransactionTypeModelData model) {

	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<TransactionType> form, SubmitType submitType) {

	}

}
